<?php

class Tabs extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Tabs':
	 * @var integer $id
	 * @var string $title
	 * @var integer $user_id
	 * @var string $session_id
	 * @var string $created
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Tabs';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id', 'numerical', 'integerOnly'=>true),
			array('title, session_id', 'length', 'max'=>32),
			array('created', 'safe'),
		);
	}

        public function behaviors() {
            return array(
                'SlugBehavior' => array(
                    'class' => 'application.components.behaviors.SlugBehavior',
                    'slug_col' => 'slug',
                    'title_col' => 'title',
                    'replace_space' => '-',
                    'overwrite' => true
                )
            );
        }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'notes'=>array(self::HAS_MANY, 'Notes', 'tab_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'title' => 'Title',
			'user_id' => 'User',
			'session_id' => 'Session',
			'created' => 'Created',
		);
	}

        public function defaultScope(){
            return array(
                'order'=>'created ASC'
            );
        }

        public function beforeSave(){
            parent::beforeSave();
            $this->created = new CDbExpression("NOW()");
            return true;
        }

         public static function loadBySlug($slug) {
            if($slug)
                $model = self::model()->find('slug=:slug', array(':slug'=>$slug));
            if($model === null)
                throw new CHttpException(404,'Nie ma takiego artykułu.');
            return $model;
        }
}